<template>
	<view class="container">
		<!-- <tui-tab :tabs="tabs" isSticky :current="currentTab" selectedColor="#E41F19" sliderBgColor="#E41F19" @change="change"></tui-tab> -->
		<!-- <tn-subsection :list="tabs" @change="change"></tn-subsection> -->
		<view class="tn-margin-top">
			<tn-subsection :list="tabs" mode="button" :borderRadius="50" @change="change"></tn-subsection>
		</view>
		<view class="" style="width: 100%;display: flex;justify-content: space-between;align-items: center;padding: 0 20rpx;">
			<view class="" style="background: #fff;display: flex;border: 1rpx solid #f6f6f6;margin: 50rpx 0;padding: 10rpx 30rpx;border-radius: 30rpx;color: #666666;"  @click="show = true">
				<view class="" style="font-size: 26rpx;">{{start}}</view>
				<view class="" style="font-size: 26rpx;" v-if="start!=end"> ~ {{end}}</view>
				<text class="tn-icon-down-triangle"></text>
			</view>
			<view class="">
				<view class="" style="display: flex;align-items: center;font-size: 28rpx;color: #999999;">
					<view class="">总收入 ￥</view>
					<view class="">{{list.income}}</view>
				</view>
				<view class="" style="display: flex;align-items: center;font-size: 28rpx;color: #999999;">
					<view class="">总支出 ￥</view>
					<view class="">{{list.expense}}</view>
				</view>
			</view>
		</view>
		<tn-calendar v-model="show" mode="range" @change="dateChange"></tn-calendar>
		<!-- <tn-button @click="show = true">打开日历</tn-button> -->
		
		<view class="tui-records__list" v-if="menuList.length">
			<view>
				<tui-list-cell :hover="false" v-for="(model,subIndex) in menuList" :key="subIndex">
					<view class="tui-records__item">
						<image class="tui-icon" :src="'https://oss.taoding1688.com/static/uniapp/static/images/mall/wallet/icon_income_3x.png'" v-if="model.amount>=0"></image>
						<image class="tui-icon" :src="'https://oss.taoding1688.com/static/uniapp/static/images/mall/wallet/icon_expend_3x.png'" v-else></image>
						
						
						<view>
							<view class="tui-title">{{model.event_text}}</view>
							<view class="tui-desc">{{model.createtime}}</view>
						</view>
						<view class="tui-right__box">
							<view class="tui-amount" :class="{'tui-expend':model.type==2}">
								{{model.amount>=0?'+':''}}{{model.amount}}</view>
							<view class="tui-desc">钱包余额: {{model.after}}</view>
						</view>
					</view>
				</tui-list-cell>
				<!--加载loading-->
			<!-- 	<tui-loadmore v-if="recordsList[index].loading" :index="3" type="red"></tui-loadmore>
				<tui-nomore v-if="!recordsList[index].pullUpOn"></tui-nomore> -->
				<!--加载loading-->
			</view>
		</view>
		
		 <tn-empty mode="list" v-else></tn-empty>

	</view>
</template>

<script>
	import {
		getUserWalletLog
	} from "@/apis/index"
	export default {
		data() {
			return {
				menuList:[],
				tabs: ['全部','收入','支出'],
				tabsList:[
					{
						name:"全部",
						type:'all'
					},
					{
						name:"收入",
						type:"income"
					},
					{
						name:"支出",
						type:"expense"
					}
				],
				list:{},
				currentTab: 0,
				recordsList: [{
					loading: false,
					pullUpOn: true,
					pageIndex: 1,
					noData: false,
					data: []
				}, {
					loading: false,
					pullUpOn: true,
					pageIndex: 1,
					noData: false,
					data: []
				}, {
					loading: false,
					pullUpOn: true,
					pageIndex: 1,
					noData: false,
					data: []
				}],
				//模拟请求返回数据
				requestData: [{
					type: 1, //1-收入 2-支出
					title: "充值收入",
					time: "2019-05-12 11:53:06",
					balance: '3006.50',
					amount: '2000.00',
					icon: 'icon_income_3x.png'
				}, {
					type: 1,
					title: "充值收入",
					time: "2019-05-12 11:53:06",
					balance: '3006.50',
					amount: '2000.00',
					icon: 'icon_income_3x.png'
				}, {
					type: 2,
					title: "交易支出",
					time: "2019-05-12 11:53:06",
					balance: '4206.50',
					amount: '2500.00',
					icon: 'icon_expend_3x.png'
				}, {
					type: 1,
					title: "充值收入",
					time: "2019-05-12 11:53:06",
					balance: '3006.50',
					amount: '2000.00',
					icon: 'icon_income_3x.png'
				}, {
					type: 1,
					title: "充值收入",
					time: "2019-05-12 11:53:06",
					balance: '3006.50',
					amount: '2000.00',
					icon: 'icon_income_3x.png'
				}, {
					type: 2,
					title: "交易支出",
					time: "2019-05-12 11:53:06",
					balance: '4206.50',
					amount: '2500.00',
					icon: 'icon_expend_3x.png'
				}, {
					type: 2,
					title: "交易支出",
					time: "2019-05-12 11:53:06",
					balance: '4206.50',
					amount: '2500.00',
					icon: 'icon_expend_3x.png'
				}, {
					type: 1,
					title: "充值收入",
					time: "2019-05-12 11:53:06",
					balance: '3006.50',
					amount: '2000.00',
					icon: 'icon_income_3x.png'
				}, {
					type: 1,
					title: "充值收入",
					time: "2019-05-12 11:53:06",
					balance: '3006.50',
					amount: '2000.00',
					icon: 'icon_income_3x.png'
				}, {
					type: 2,
					title: "交易支出",
					time: "2019-05-12 11:53:06",
					balance: '4206.50',
					amount: '2500.00',
					icon: 'icon_expend_3x.png'
				}],
				show: false,
				obj: {
					type: 'money',
					tab: 'all',
					list_rows: 10,
					page: 1,
					createTime: '2024-09-14+00:00:00',
					endTime: '2024-09-14+23:59:59'
				},
				start: '',
				end: '',
				last_page:null
			}
		},
		async onLoad() {
			// this.getRecordsList(this.currentTab)
			let str=await this.getNowDate()
			this.obj.createTime = str + '+00:00:00'
			this.obj.endTime = str + '+23:59:59'
			this.obj.tab=this.tabsList[0].type
			this.start =str
			this.end = str
			this.getList()
		},
		methods: {
			dateChange(e) {
					this.menuList=[]
				this.start = e.startDate
				this.end = e.endDate
				this.getList()
			},
			getNowDate() {
			      let date = new Date();
			      let year = date.getFullYear();
			      let month = date.getMonth() + 1; // 月份是从0开始的，所以需要加1
			      let day = date.getDate();
			
			      // 如果月份或日期小于10，则前面补0
			      month = month > 9 ? month : '0' + month;
			      day = day > 9 ? day : '0' + day;
			
			      let currentDate = `${year}-${month}-${day}`;
				  return currentDate
			    },
			getList() {
				this.obj.createTime = this.start + '+00:00:00'
				this.obj.endTime = this.end + '+23:59:59'
				getUserWalletLog(this.obj).then(res => {
					this.list=res.data
					
					// this.menuList=res.data.list.data
					
					
					this.menuList=[...this.menuList,...res.data.list.data]
					
					
					
					
					
					this.last_page=res.data.list.last_page
					
					
					
					
				})
			},
			change(e) {
				console.log(e)
				// this.tabsList = e.index
				this.obj.page=1
				this.obj.tab=this.tabsList[e.index].type
				this.menuList=[]
				this.getList()
				// if (this.recordsList[this.currentTab].pageIndex == 1) {
				// 	this.getRecordsList(this.currentTab)
					
				// }
			},
			getRecordsList(index, refresh) {
				let item = this.recordsList[index]
				if (!refresh) {
					item.loading = true;
				}
				setTimeout(() => {
					refresh && uni.stopPullDownRefresh();
					let recordsList = [...this.requestData];
					if (this.currentTab > 0) {
						recordsList = recordsList.filter(item => item.type === this.currentTab)
					}
					if (item.pageIndex == 1) {
						item.data = recordsList;
					} else {
						item.data = item.data.concat(recordsList);
					}
					if (item.pageIndex > 2 || recordsList.length < 10) {
						item.pullUpOn = false;
					}
					item.pageIndex++;
					item.loading = false;
				}, 0)
			}
		},
		onPullDownRefresh() {
			// let index = this.currentTab
			// let item = this.recordsList[index]
			// item.pageIndex = 1;
			// item.loading = false;
			// item.pullUpOn = true;
			// item.noData = false;
			// this.getRecordsList(index, true)
		},
		onReachBottom() {
			this.obj.page+=1
			
			if (this.last_page>=this.obj.page) {
				this.getList()
			}
			
			
			
			
			// let index = this.currentTab
			// if (!this.recordsList[index].pullUpOn) return;
			// this.getRecordsList(index)
		}
	}
</script>

<style>
	.container {
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tui-records__list {
		margin-top: 20rpx;
	}

	.tui-records__item {
		width: 100%;
		display: flex;
		align-items: center;
		padding: 10rpx 0;
	}

	.tui-icon {
		width: 72rpx;
		height: 72rpx;
		margin-right: 20rpx;
	}

	.tui-title {
		font-size: 30rpx;
		font-weight: 400;
		color: #333333;
	}

	.tui-desc {
		font-size: 24rpx;
		font-weight: 400;
		color: #888888;
		padding-top: 12rpx;
	}

	.tui-right__box {
		margin-left: auto;
		text-align: right;
	}

	.tui-amount {
		font-size: 30rpx;
		font-weight: 400;
		color: #EB0909;
	}

	.tui-expend {
		color: #19be6b !important;
	}
</style>